package com.woniu.controller;

import com.woniu.constant.Constant;
import com.woniu.entity.Result;
import com.woniu.entity.vo.UserAppeal5VO;
import com.woniu.entity.vo.UserAppeal6VO;
import com.woniu.entity.vo.UserAppeal7VO;
import com.woniu.pojo.Appeal;
import com.woniu.pojo.Order;
import com.woniu.service.AppealService;
import com.woniu.service.OrderService;
import com.woniu.utils.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author Ma.Lei
 * @description
 * @date 2022/12/28 19:33
 */
@RestController
@RequestMapping("/userAppeal")
@Api(tags = "用户申诉控制类")
public class AppealController extends BaseController {

    @Autowired
    private AppealService appealService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private OrderService orderService;

    @ApiOperation("订单申诉内容")
    @GetMapping("/selectAppealInfo/{orderCode}/{status}")
    public Result selectAppealInfo(@PathVariable String orderCode,@PathVariable String status){
        if ("5".equals(status)){
            UserAppeal5VO userAppeal5VO = appealService.selectAppeal5(orderCode);
            return toDataResult(userAppeal5VO);
        }
        if ("6".equals(status)){
            UserAppeal6VO userAppeal6VO = appealService.selectAppeal6(orderCode);
            return toDataResult(userAppeal6VO);
        }
        if ("7".equals(status)){
            UserAppeal7VO userAppeal7VO = appealService.selectAppeal7(orderCode);
            return toDataResult(userAppeal7VO);
        }
        return new Result(500,"未找到数据");
    }
    @ApiOperation("新增用户申诉")
    @PostMapping("addAppeal/{orderCode}")
    @Transactional
    public Result addAppeal(@RequestBody Appeal appeal,@PathVariable String orderCode){
        Order order = orderService.selectByOrderCode(orderCode);
        Boolean saveFlag = null;
        if ("5".equals(order.getOrderStatus())){
            return new Result(500,"该订单正在申诉,请勿重复申诉");
        }
        if ("3".equals(order.getOrderStatus())){
            appeal.setOrderId(order.getId());
            appeal.setStatus("1");
            order.setOrderStatus("5");
            orderService.updateById(order);
            saveFlag = appealService.save(appeal);
            if (saveFlag){
                stringRedisTemplate.opsForSet().add(Constant.REDIS_DB_UPLOAD_IMAGE,appeal.getImageUrl());
            }
        }
        return toResult(saveFlag);
    }
}
